package com.java.servlet.web.commons;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

public class PropertiesInitServlet extends HttpServlet {
    private Properties viewConfig;
	private static final long serialVersionUID = 1L;

	public void init(ServletConfig config) throws ServletException {
		System.out.println("PropertiesInitServlet is initializing Properties");
		String viewLocation = config.getInitParameter("view-properties-location");

		ServletContext sc = config.getServletContext();

		if ("".equals(viewLocation) || viewLocation == null) {
			System.err.println("*** No Properties-properties-location init param, so initializing Properties");
			throw new ServletException("Config param not found");
//			BasicConfigurator.configure();
		} else {
			viewConfig = new Properties();
             InputStream is = config.getServletContext().getResourceAsStream(viewLocation);
             String webAppPath = sc.getRealPath("/");
 			 String viewProp = webAppPath + viewLocation;
            try{
            	System.out.println("Initializing Properties with: " + viewProp);
            	viewConfig.load(is);

//                Iterator keyIterator = configurationProperties.keySet().iterator();
//                while (keyIterator.hasNext()) {
//                        String key = (String) keyIterator.next();
//                        String propertyValue = configurationProperties.getProperty(key);
//                        sc.log(key + " - " + propertyValue);
//                        System.out.println(key + " - " + propertyValue);
//                }                       

                sc.setAttribute("config", config);

                is.close();
            }catch(Exception e){
            	throw new ServletException("Error loading config.", e);
            }
			/*String webAppPath = sc.getRealPath("/");
			String viewProp = webAppPath + viewLocation;
			File yoMamaYesThisSaysYoMama = new File(viewProp);
			if (yoMamaYesThisSaysYoMama.exists()) {
				System.out.println("Initializing Properties with: " + viewProp);
				getPropertiesFromFile(viewLocation);
			} else {
				System.err.println("*** " + viewProp + " file not found, so initializing Properties with BasicConfigurator");
//				BasicConfigurator.configure();
			}*/
		}
		super.init(config);
	}
    public static Properties getPropertiesFromFile(String pathFile) {
        Properties propiedades = new Properties();
        try {
            URL url = PropertiesUtils.class.getResource(pathFile);
            if (url != null) {
                propiedades.load(url.openStream());
            } else {
                return null;
            }
        } catch (IOException ex) {
        	System.out.println("Error al cargar el archivo de propiedades" + ex);
        }
        return propiedades;
    }

    public static Properties getPropertiesFromFile(URL url) {
        Properties propiedades = new Properties();
        try {
            if (url != null) {
                propiedades.load(url.openStream());
            } else {
                return null;
            }
        } catch (IOException ex) {
        	System.out.println("Error al cargar el archivo de propiedades" + ex);
        }
        return propiedades;
    }
    
}